package com.yuerlive.rainbow.module.mine;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;
import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.base.BaseActivity;
import com.yuerlive.rainbow.common.RecycleViewItemClickListener;
import com.yuerlive.rainbow.entity.Anchor;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.UserInfo;
import com.yuerlive.rainbow.module.homepage.HomePageActivity;
import com.yuerlive.rainbow.module.mine.adapter.MyFansAdapter;
import com.yuerlive.rainbow.module.mine.presenter.MyFansContract;
import com.yuerlive.rainbow.module.mine.presenter.MyFansPresenter;
import com.yuerlive.rainbow.observer.Observerable;
import com.yuerlive.rainbow.observer.ObserverableType;
import com.yuerlive.rainbow.utils.GlideUtil;
import com.yuerlive.rainbow.utils.Utils;
import com.yuerlive.rainbow.widget.CircleImageView;
import com.yuerlive.rainbow.widget.CustomDialog;
import com.yuerlive.rainbow.widget.timeselector.TextUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by xiaoyi on 2017/5/18.
 */

public class MyFansActivity extends BaseActivity implements MyFansContract.View, RecycleViewItemClickListener {

    @BindView(R.id.pullLoadMoreRecyclerView)
    PullLoadMoreRecyclerView refresh;
    List<Anchor> mDatas = new ArrayList<>();
    MyFansAdapter adapter;
    MyFansContract.Presenter presenter;
    private boolean isLoad;

    CustomDialog userCard;
    //用户卡片
    TextView tvNickName;
    TextView tvLevel;
    TextView tvId;
    TextView tvWords;
    TextView tvFollowCount;
    TextView tvFollow;
    TextView tvFansCount;
    TextView tvCharmCount;
    LinearLayout llCount;
    CircleImageView imgHeader;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mymessage;
    }

    @Override
    protected void initViews() {
        presenter = new MyFansPresenter(this);
        presenter.getMyFans(false);
        setTitle("我的粉丝");
        refresh.setColorSchemeResources(R.color.colorPrimary);
        refresh.setLinearLayout();
        refresh.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                mDatas.clear();
                presenter.getMyFans(false);
            }

            @Override
            public void onLoadMore() {
                if (!isLoad) {
                    isLoad = true;
                    presenter.getMyFans(true);
                }
            }
        });
        adapter = new MyFansAdapter(this, mDatas);
        refresh.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        initUserCard();
    }

    @Override
    public void setPresenter(MyFansContract.Presenter presenter) {
        super.presenter = presenter;
    }

    @Override
    public void showLoading() {
        refresh.setRefreshing(false);
    }

    @Override
    public void hideLoading() {
        refresh.setPullLoadMoreCompleted();
    }

    @Override
    public void showFansList(List<Anchor> fansList, int page) {
        if (page == 1) {
            mDatas.clear();
        }
        mDatas.addAll(fansList);
        adapter.notifyDataSetChanged();
        isLoad = false;
    }

    private void initUserCard() {
        if (userCard == null) {
            View v = LayoutInflater.from(mActivity).inflate(R.layout.layout_usercard, null);
            userCard = new CustomDialog(mActivity, v, true);
            imgHeader = (CircleImageView) v.findViewById(R.id.imgHeader);
            tvCharmCount = (TextView) v.findViewById(R.id.tvCharmCount);
            tvFansCount = (TextView) v.findViewById(R.id.tvFansCount);
            tvId = (TextView) v.findViewById(R.id.tvId);
            tvNickName = (TextView) v.findViewById(R.id.tvNickName);
            tvWords = (TextView) v.findViewById(R.id.tvWords);
            tvLevel = (TextView) v.findViewById(R.id.tvLevel);
            tvFollowCount = (TextView) v.findViewById(R.id.tvFollowCount);
            tvFollow = (TextView) v.findViewById(R.id.tvFollow);
            v.findViewById(R.id.ivCancel).setOnClickListener((view) ->
                    userCard.hide());
            userCard.hide();

        }
    }

    @Override
    public void onItemClick(View view, int position) {
        switch (view.getId()) {
            case R.id.tvFollow:
                presenter.subscribeUp(position, mDatas.get(position).getId());
                break;
            case R.id.imgHeader:
              //  presenter.getUserInfo(position, mDatas.get(position).getId());
                Intent i=new Intent();
                i.setClass(mActivity, HomePageActivity.class);
                i.putExtra("otherId",mDatas.get(position).getId());
                jumpToActivity(i);
                break;
        }
    }
    @Override
    public void showUserInfo(BaseData<UserInfo> data, int pos) {
        userCard.show();
        UserInfo info = data.getObject();
        tvWords.setText(TextUtil.isEmpty(info.getPersonalsignature()) ? "这家伙很懒，啥都没说~"
                : info.getPersonalsignature());
        tvCharmCount.setText(String.valueOf(info.getCharmtotalvalue()));
        tvFansCount.setText(String.valueOf(info.getFans()));
        tvFollowCount.setText(String.valueOf(info.getSubscribenum()));
        tvId.setText(String.valueOf(info.getId()));
        tvNickName.setText(info.getNickname());
        tvNickName.setCompoundDrawablesWithIntrinsicBounds(0, 0, info.getSex() == 0 ? R.drawable.icon_boy : R.drawable.icon_girl, 0);
        tvLevel.setText(String.valueOf(info.getLv()));
        Utils.setLevel(tvLevel, info.getLv());
        GlideUtil.load(this, info.getIcon(), imgHeader);
        if (info.getIsSubscribe() == 0) {
            tvFollow.setText("+ 关注");
            tvFollow.setTextColor(getResources().getColor(R.color.white));
            tvFollow.setBackgroundResource(R.drawable.btn_recharge);
        } else {
            tvFollow.setText("已关注");
            tvFollow.setTextColor(getResources().getColor(R.color.text_livecard_Id));
            tvFollow.setBackgroundResource(R.drawable.btn_recharge_gray);
        }
        tvFollow.setOnClickListener((view) -> {
            presenter.subscribeUp(pos, mDatas.get(pos).getId());
            userCard.hide();
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        userCard.dismiss();
    }

    @Override
    public void showSubscribeResult(int code, int pos) {
        Anchor anchor = mDatas.get(pos);
        if (code == 0) {
            anchor.setIsSubscribe((anchor.getIsSubscribe() + 1) % 2);
            if (anchor.getIsSubscribe() == 1) {
                showToast("关注成功");
                Observerable.getInstance().notifyChange(ObserverableType.ATTENTION, 1);
                mDatas.set(pos, anchor);
                if (userCard.isShowing()) {
                    tvFollow.setText("已关注");
                    tvFollow.setTextColor(getResources().getColor(R.color.text_livecard_Id));
                    tvFollow.setBackgroundResource(R.drawable.btn_recharge_gray);
                    tvFansCount.setText(String.valueOf(
                            Integer.parseInt(tvFansCount.getText().toString()) + 1
                    ));
                }
            } else {
                showToast("取消关注成功");
                Observerable.getInstance().notifyChange(ObserverableType.ATTENTION, -1);
                if (userCard.isShowing()) {
                    tvFollow.setText("+ 关注");
                    tvFollow.setTextColor(getResources().getColor(R.color.white));
                    tvFollow.setBackgroundResource(R.drawable.btn_recharge);
                    tvFansCount.setText(String.valueOf(
                            Integer.parseInt(tvFansCount.getText().toString()) - 1
                    ));
                }
            }


            adapter.notifyItemChanged(pos);
        } else {
            if (anchor.getIsSubscribe() == 1) {
                showToast("关注失败");
            } else {
                showToast("取消关注失败");
            }
        }
    }
}
